View Javadoc

1   package net.sourceforge.simplegamenet.connectaline;
2   
3   import java.io.Serializable;
4   
5   
6   public class CALPlayField implements Serializable {
7   
8       private CALSquare[][] squares;
9       private int cALNumberOfColumns;
10      private int cALNumberOfRows;
11      private int cALLengthOfLine;
12      private int participantAtTurnIndex = 0;
13      private Integer[] participantsOrder;
14  
15      CALPlayField(int cALNumberOfColumns, int cALNumberOfRows, int cALLengthOfLine,
16                   Integer[] participantsOrder) {
17          this.cALNumberOfColumns = cALNumberOfColumns;
18          this.cALNumberOfRows = cALNumberOfRows;
19          this.cALLengthOfLine = cALLengthOfLine;
20          this.participantsOrder = participantsOrder;
21          squares = new CALSquare[cALNumberOfColumns][cALNumberOfRows];
22          for (int i = 0; i < cALNumberOfColumns; i++) {
23              for (int j = 0; j < cALNumberOfRows; j++) {
24                  squares[i][j] = new CALSquare();
25              }
26          }
27      }
28  
29      public boolean isMoveAllowedGravity(int x, int y) {
30          if (y == cALNumberOfRows - 1 && squares[x][y].getOccupied() == false) {
31              return true;
32          } else if (y != cALNumberOfRows - 1 && squares[x][y + 1].getOccupied() == true
33                  && squares[x][y].getOccupied() == false) {
34              return true;
35          } else {
36              return false;
37          }
38      }
39  
40      public boolean isMoveAllowedAnyEmptySpot(int x, int y) {
41          return !squares[x][y].getOccupied();
42      }
43  
44      public boolean isWinner(int x, int y) {
45          int hline = 1;
46          int vline = 1;
47          int dline1 = 1;
48          int dline2 = 1;
49          boolean end = false;
50          // horizontal right
51          for (int i = 1; i < cALLengthOfLine && (x + i) < cALNumberOfColumns
52                  && !end; i++) {
53              if (squares[x][y].getPlayerID() == squares[x + i][y].getPlayerID()) {
54                  hline += 1;
55              } else {
56                  end = true;
57              }
58          }
59          // horizontal left
60          end = false;
61          for (int i = 1; i < cALLengthOfLine && hline != cALLengthOfLine
62                  && (x - i) >= 0 && !end; i++) {
63              if (squares[x][y].getPlayerID() == squares[x - i][y].getPlayerID()) {
64                  hline += 1;
65              } else {
66                  end = true;
67              }
68          }
69          // vertical up
70          end = false;
71          for (int i = 1; i < cALLengthOfLine && vline != cALLengthOfLine
72                  && (y - i) >= 0 && !end; i++) {
73              if (squares[x][y].getPlayerID() == squares[x][y - i].getPlayerID()) {
74                  vline += 1;
75              } else {
76                  end = true;
77              }
78          }
79          // vertical down
80          end = false;
81          for (int i = 1; i < cALLengthOfLine && vline != cALLengthOfLine
82                  && (y + i) < cALNumberOfRows && !end; i++) {
83              if (squares[x][y].getPlayerID() == squares[x][y + i].getPlayerID()) {
84                  vline += 1;
85              } else {
86                  end = true;
87              }
88          }
89          // diagonal from upper left to lower right
90          // upper left
91          end = false;
92          for (int i = 1; i < cALLengthOfLine && dline1 != cALLengthOfLine
93                  && (x - i) >= 0 && (y - i) >= 0 && !end; i++) {
94              if (squares[x][y].getPlayerID() == squares[x - i][y - i].getPlayerID()) {
95                  dline1 += 1;
96              } else {
97                  end = true;
98              }
99          }
100         // lower right
101         end = false;
102         for (int i = 1; i < cALLengthOfLine && dline1 != cALLengthOfLine
103                 && (x + i) < cALNumberOfColumns && (y + i) < cALNumberOfRows && !end; i++) {
104             if (squares[x][y].getPlayerID() == squares[x + i][y + i].getPlayerID()) {
105                 dline1 += 1;
106             } else {
107                 end = true;
108             }
109         }
110         // diagonal from upper right to lower left
111         // upper right
112         end = false;
113         for (int i = 1; i < cALLengthOfLine && dline2 != cALLengthOfLine
114                 && (x + i) < cALNumberOfColumns && (y - i) >= 0 && !end; i++) {
115             if (squares[x][y].getPlayerID() == squares[x + i][y - i].getPlayerID()) {
116                 dline2 += 1;
117             } else {
118                 end = true;
119             }
120         }
121         // lower left
122         end = false;
123         for (int i = 1; i < cALLengthOfLine && dline2 != cALLengthOfLine
124                 && (x - i) >= 0 && (y + i) < cALNumberOfRows && !end; i++) {
125             if (squares[x][y].getPlayerID() == squares[x - i][y + i].getPlayerID()) {
126                 dline2 += 1;
127             } else {
128                 end = true;
129             }
130         }
131         if (hline == cALLengthOfLine || vline == cALLengthOfLine
132                 || dline1 == cALLengthOfLine || dline2 == cALLengthOfLine) {
133             return true;
134         } else {
135             return false;
136         }
137     }
138 
139     public boolean isRemise() {
140         boolean remise = false;
141         int aantal = 0;
142         for (int i = 0; i < cALNumberOfColumns; i++) {
143             for (int j = 0; j < cALNumberOfRows; j++) {
144                 if (squares[i][j].getOccupied() == true) {
145                     aantal++;
146                 }
147             }
148         }
149         if (aantal == (cALNumberOfColumns) * (cALNumberOfRows)) {
150             remise = true;
151             return remise;
152         } else {
153             remise = false;
154             return remise;
155         }
156     }
157 
158 
159     public void makeMove(int x, int y, Integer playerID) {
160         squares[x][y].setPlayerID(playerID);
161         participantAtTurnIndex = (participantAtTurnIndex + 1)
162                 % participantsOrder.length;
163     }
164 
165     public Integer getSquarePlayerID(int i, int j) {
166         return squares[i][j].getPlayerID();
167     }
168 
169     public boolean isOccupied(int i, int j) {
170         return squares[i][j].getOccupied();
171     }
172 
173     public Integer getParticipantAtTurnPlayerID() {
174         return participantsOrder[participantAtTurnIndex];
175     }
176 
177     public Integer[] getParticipantsOrder() {
178         return participantsOrder;
179     }
180 }